/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.state;

import deatax.GameData;
import deatax.Highlight;
import deatax.Improvement;
import deatax.Settlement;
import deatax.Tile;
import deatax.network.Client;
import deatax.network.Messages;
import deatax.network.NetworkListener;
import deatax.unit.UnitStack;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

/**
 *
 * @author pson
 */
public class BuildingPlaceState extends GameState implements NetworkListener {

    BuildingPlaceMouseListener listener;
    private UnitStack stack;
    private Improvement improve;
    ArrayList<Tile> valid;

    public BuildingPlaceState(GameData data, UnitStack stack, Improvement improve) {
        super("Building Place", data);
        this.improve = improve;
        this.stack = stack;

        valid = new ArrayList<Tile>();
        data.getMap().clearHighlights();
        Highlight highlight = new Highlight();
        for (Tile neigh : stack.getTile().getNeighbours()) {
            if (neigh.getTerrain().getImprovements().contains(data.getGame().getImprove("Road"))
                    && neigh.getImprovement() == null && (neigh.getOwner() == null || neigh.getOwner() == stack.getOwner())) {
                valid.add(neigh);
            }
        }
        valid.add(stack.getTile());
        int[] area = new int[valid.size() * 2];
        int i = 0;
        for (Tile move : valid) {
            area[i] = move.getX();
            area[i + 1] = move.getY();
            i += 2;
        }
        highlight.tiles = area;
        highlight.colour = new Color(192, 128, 255);
        data.getMap().addHighlight(highlight);
    }

    @Override
    public void begin() {

        listener = new BuildingPlaceMouseListener();
        data.getDisplay().addMouseListener(listener);
    }

    @Override
    public void finish() {
        //System.out.println("exiting");
        data.getMap().clearHighlights();
        data.getState().popState();
    }

    @Override
    public void die() {
        if (listener != null) {
            data.getDisplay().removeMouseListener(listener);
        }
    }

    @Override
    public void processMessage(int type, String[] arg) {
        commonMessages(type, arg);
    }

    protected class BuildingPlaceMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1) {
                Tile t = data.getRender().getHover();
                if (t != null && valid.contains(t)) {
                    Client.get().sendMessage(Messages.CLAIM, new String[]{stack.getOwner().getName(), String.valueOf(t.getX()), String.valueOf(t.getY())});
                    Client.get().sendMessage(Messages.BUILD, new String[]{improve.getName(), String.valueOf(t.getX()), String.valueOf(t.getY())});
                    if (improve instanceof Settlement) {
                        Client.get().sendMessage(Messages.UNIT, new String[]{stack.getOwner().getName(),
                                    stack.getType().getName(), "-1", String.valueOf(stack.getTile().getX()), String.valueOf(stack.getTile().getY())});
                    }
                    Client.get().sendMessage(Messages.LOG, new String[]{stack.getOwner().getName(), "Built a " + improve.getName()});
                    finish();

                }
            } else if (e.getButton() == MouseEvent.BUTTON3) {
                finish();
            }
        }
    }
}
